package com.example.androidexample.animation;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;

import com.example.androidexample.R;
import com.example.basic.base.BaseActivity;

import java.util.ArrayList;

public class AnimationActivity extends BaseActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private static final String TAG = "AnimationActivity";

    private View view_show;
    private AnimationBasicParamsProvider basicParamsProvider;
    private final ArrayList<Animation> animations = new ArrayList<>();

    private RotateAnimationBuilder rotateAnimationBuilder;
    private TranslateAnimationBuilder translateAnimationBuilder;
    private AlphaAnimationBuilder alphaAnimationBuilder;
    private ScaleAnimationBuilder scaleAnimationBuilder;

    public void handleAction(AnimationBuilderType type, AnimationBuilder action) {
        Log.d(TAG, "handleAction: type = " + type.name());
        startViewAnimation(fixAnimation(action.buildAnimation()));
    }

    private void startViewAnimation(Animation fixAnimation) {
        if (fixAnimation == null) {
            Log.w(TAG, "startViewAnimation: fixAnimation 不能为空");
            return;
        }

        if (view_show.getAnimation() != null) {
            view_show.getAnimation().cancel();
        }
        view_show.startAnimation(fixAnimation);
    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        view_show = findViewById(R.id.view_show);

        AnimationPivotParamsProvider pivotParamsBuilder = new AnimationPivotParamsProvider(this);
        basicParamsProvider = new AnimationBasicParamsProvider(this);

        rotateAnimationBuilder = new RotateAnimationBuilder(this, pivotParamsBuilder);
        translateAnimationBuilder = new TranslateAnimationBuilder(this, pivotParamsBuilder);
        alphaAnimationBuilder = new AlphaAnimationBuilder(this);
        scaleAnimationBuilder = new ScaleAnimationBuilder(this, pivotParamsBuilder);
        new XMLAnimationBuilder(this);

        CheckBox ckMultiAlpha = findViewById(R.id.ck_multi_alpha);
        CheckBox ckMultiRotate = findViewById(R.id.ck_multi_rotate);
        CheckBox ckMultiTranslate = findViewById(R.id.ck_multi_translate);
        CheckBox ckMultiScale = findViewById(R.id.ck_multi_scale);
        ckMultiAlpha.setOnCheckedChangeListener(this);
        ckMultiRotate.setOnCheckedChangeListener(this);
        ckMultiTranslate.setOnCheckedChangeListener(this);
        ckMultiScale.setOnCheckedChangeListener(this);

        Button bt_stop = findViewById(R.id.bt_stop);
        bt_stop.setOnClickListener(this);

        Button btStartAnimationSet = findViewById(R.id.bt_start_animation_set);
        btStartAnimationSet.setOnClickListener(this);
    }

    @Override
    protected int setLayoutXmlid() {
        return R.layout.activity_animation;
    }

    @Override
    public String getTag() {
        return "AnimationActivity";
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_stop:{
                Animation viewAnimation = view_show.getAnimation();
                if (viewAnimation instanceof AnimationSet) {
                    ArrayList<Animation> animations = (ArrayList<Animation>) ((AnimationSet) viewAnimation).getAnimations();
                    for (Animation animation : animations) {
                        animation.cancel();
                    }
                } else {
                    if (viewAnimation != null) {
                        viewAnimation.cancel();
                    }
                }
            } break;
            case R.id.bt_start_animation_set:{
                startAnimationSet();
            } break;
        }
    }

    private void startAnimationSet() {
        AnimationSet set = new AnimationSet(false);
        for (Animation animation : animations) {
            set.addAnimation(fixAnimation(animation));
        }
        startViewAnimation(set);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        @IdRes
        int viewId = buttonView.getId();
        Animation animation = null;
        switch (viewId) {
            case R.id.ck_multi_alpha: {
                animation = alphaAnimationBuilder.buildAnimation();
            } break;
            case R.id.ck_multi_rotate: {
                animation = rotateAnimationBuilder.buildAnimation();
            } break;
            case R.id.ck_multi_translate: {
                animation = translateAnimationBuilder.buildAnimation();
            } break;
            case R.id.ck_multi_scale: {
                animation = scaleAnimationBuilder.buildAnimation();
            } break;
        }
        if (isChecked) {
            animations.add(animation);
        } else {
            animations.remove(animation);
        }
        Log.d(TAG, "onCheckedChanged: animations.size = " + animations.size());
    }

    private Animation fixAnimation(Animation buildAnimation) {
        long mDuration = basicParamsProvider.getDuration();
        boolean fillAfter = basicParamsProvider.isAfterFill();
        int repeatConut = basicParamsProvider.getRepeatCount();
        int repeatMode = basicParamsProvider.getRepeatMode();

        buildAnimation.setRepeatCount(repeatConut);
        buildAnimation.setRepeatMode(repeatMode);
        buildAnimation.setDuration(mDuration);
        buildAnimation.setFillAfter(fillAfter);
        return buildAnimation;
    }
}
